<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    .box {
      width: 500px;
      height: 500px;
      border: 2px solid red;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -250px;
      margin-top: -250px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-evenly;
      align-content: space-evenly;
    }


    li {
      width: 150px;
      height: 150px;
      font-size: 12px;
      text-align: center;
      /* border: 1px solid red; */
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
    }

    .img-box {
      display: flex;
      height: 120px;
    }

    img {
      width: 100%;
    }

    .active {
      background-color: yellow;
      border: 5px solid orange;
      transform: scale(1.1);
    }

    .begin {
      background-color: brown;
      color: white;
    }

    .prize_box {
      position: absolute;
      left: 100px;
      top: 200px;
      overflow: hidden;
      width: 155px;
      height: 148px;
      border: 1px solid red;
    }

    .prize {
      transform: translateY(150px);
    }



    .prize_txt {
      position: absolute;
      top: 150px;
      left: 300px;
      width: 100px;
      height: 20px;
      border: 1px solid red;
    }
  </style>
</head>

<body>

  <div class="web">


    <div class="prize_box">
      <ul class="prize">
        <li><img src="" alt=""></li>
        <li><img src="" alt=""></li>
        <li><img src="" alt=""></li>
        <li><img src="" alt=""></li>
        <li><img src="" alt=""></li>
        <li><img src="" alt=""></li>
        <li><img src="" alt=""></li>
      </ul>
    </div>





    <div class="prize_txt"></div>
    <ul class="box">
      <li>
        <div class="img-box"><img src="./img/20.png" alt=""></div>
        <p>01奖品</p>
      </li>
      <li>
        <div class="img-box"><img src="./img/21.png" alt=""></div>
        <p>02奖品</p>
      </li>
      <li>
        <div class="img-box"><img src="./img/22.png" alt=""></div>
        <p>03奖品</p>
      </li>
      <li>
        <div class="img-box"><img src="./img/23.png" alt=""></div>
        <p>04奖品</p>
      </li>
      <li class="begin">抽奖</li>
      <li>
        <div class="img-box"><img src="./img/24.png" alt=""></div>
        <p>05奖品</p>
      </li>
      <li>
        <div class="img-box"><img src="./img/25.png" alt=""></div>
        <p>06奖品</p>
      </li>
      <li>
        <div class="img-box"><img src="./img/26.png" alt=""></div>
        <p>07奖品</p>
      </li>
      <li>
        <div class="img-box"><img src="" alt=""></div>
        <p>谢谢哦</p>
      </li>
    </ul>
  </div>



  <script>
    let beginBtn = document.querySelector(".begin")
    let lis = document.querySelectorAll(".box>li:not(.begin)")
    let lastindex = 0;
    let nowindex = 0;
    let pathArr = [0, 1, 2, 4, 7, 6, 5, 3]
    let circle = 80;
    let timer = null;
    let isRun = false;
    let speed = 200;

    let prize = document.querySelector(".prize");
    let prize_li = document.querySelectorAll(".prize li");
    // let liHeight = getComputedStyle(prize).height;

    let liHeight = getComputedStyle(prize.firstElementChild).height;

    let prize_txt = document.querySelector(".prize_txt");
    let img_box = document.querySelectorAll(".img-box");








    // console.log([...lis[pathArr[nowindex % 8]].children][0].firstChild.getAttribute("src"));



    beginBtn.addEventListener("click", () => {

      if (!isRun) {
        isRun = true;
        nowindex = lastindex = 0;
        // nowindex = 0;
        circle = Math.floor(Math.random() * 8 + 80)
        prize.style.transform = `translateY(50px)`;

        run();
      }
    });

    function run() {
      lis[pathArr[lastindex]].classList.remove("active")
      lis[pathArr[nowindex % 8]].classList.add("active")
      lastindex = nowindex % 8;

      let img = [...lis[pathArr[nowindex % 8]].children][0].firstChild.getAttribute("src");
      [...prize_li].forEach((item) => {
        [...[item][0].children][0].setAttribute("src", img)
      })

      // [...prize.children][0].setAttribute("src", img);
      // prize.forEach((item) => {
      //   [...item.children][0].setAttribute("src", img);
      // })




      prize_txt.innerText = lis[pathArr[nowindex % 8]].lastElementChild.textContent;

      if (nowindex <= 30 && speed > 40) {
        speed -= 25
      }
      if (nowindex > 60 && speed < 400) {
        speed += 15
      }

      lunbo();
      if (nowindex === circle) {
        // clearInterval(timer)
        isRun = false;
      } else {
        setTimeout(run, speed)
      }
      nowindex++;




      // (nowindex === pathArr.length) && (nowindex = 0)
    }

    // let img = [...lis[pathArr[nowindex % 8]].children][nowindex % 8].firstChild.getAttribute("src");
    // console.log([...prize.children][0].setAttribute("src", img));

    // let img = [...img_box][0].children[0].getAttribute("src");
    // [...prize.children][0].setAttribute("src", img)
    // let img = [...lis[pathArr[nowindex]].children][nowindex].firstChild.getAttribute("src")
    // console.log(img);

    // prize_txt.innerText = lis[pathArr[nowindex]].lastElementChild.textContent;

    function lunbo() {
      if (nowindex) {
        if (speed > 80) {
          prize.style.transition = `all 40ms`;
          prize.style.transform = `translateY(-${liHeight})`;
        } else {
          prize.appendChild(prize.firstElementChild);
        }
      } else {
        prize.style.transform = `translateY(0px)`;
      }
    }

    prize.addEventListener('transitionend', () => {
      if (nowindex > 1) {
        prize.appendChild(prize.firstElementChild);
        prize.setAttribute('style', 'transform:translateY(0px)');
      }
    });

    prize.addEventListener('transitionend', () => {
      if (nowindex > 1) {
        prize.appendChild(prize.firstElementChild);
        prize.setAttribute('style', 'transform:translateY(0px)');
      }
    });


  </script>
</body>

</html>